import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.sound.midi.MidiDevice.Info;

import com.damon.utils.DbUtil;

public class InsertData {
	public static void main(String[] args) throws SQLException {
		Connection connection = DbUtil.getConnection();
		//添加设备数据
//		String insertDeviceSql = "insert into print_device(device_number,ip,port) values(?,?,?)";
//		PreparedStatement insertDevicePs = connection.prepareStatement(insertDeviceSql);
//		
//		for(int i=1;i<=20;i++) {
//			insertDevicePs.setString(1, "p"+i);
//			insertDevicePs.setString(2, "127.0.0.1");
//			insertDevicePs.setInt(3, 9000+i);
//			insertDevicePs.executeUpdate();
//		}
//		insertDevicePs.close();
		//查询设备数据
		ArrayList<ArrayList<String>> list = new ArrayList<>();
		
		String sql = "select ip,port from print_device where is_connection=1";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next()) {
			String ip = rs.getString(1);
			String port = String.valueOf(rs.getInt(2));
			ArrayList<String> info = new ArrayList<>();
			info.add(ip);
			info.add(String.valueOf(port));
			list.add(info);
		}
		System.out.println(list);
		ps.close();
		//添加数据
		String insertDataSql = "insert into print_task(ip,port,destination,departure_place,barcode,simple_barcode,to_pos,number)"
				+"VALUES(?,?,?,?,?,?,?,?)";
		PreparedStatement insertDataPs = connection.prepareStatement(insertDataSql);
		int count = 10000;
		while(count>0) {
			for(ArrayList<String> l : list) {
				String ip = l.get(0);
				int port = Integer.valueOf(l.get(1));
				
				insertDataPs.setString(1, ip);
				insertDataPs.setInt(2, port);
				insertDataPs.setString(3, "测试");
				insertDataPs.setString(4, "测试");
				insertDataPs.setString(5, "测试");
				insertDataPs.setString(6, "测试");
				insertDataPs.setString(7, "测试");
				insertDataPs.setInt(8, 100);
				insertDataPs.executeUpdate();
			}
			count--;
		}
//		connection.close();
		
	}
}
